<?php
namespace Home\Service;

use Org\Util\String;
use Think\Service\BasicService;

class PayService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function AddAll($model) {
        $data = $this->db->addAll($model);
        return $data;
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($where, $model = array()) {
        return $this->db->where($where)->save($model);
    }

    public function find($condition) {
        return $this->db->where($condition)->find();
    }

    public function query($condition, $limit = array(), $field = '*', $order = '', $join = '') {
        $data = $this->db->join($join)->where($condition)->field($field)->limit($limit['start'], $limit['end'])->order($order)->select();
        return $data;
    }

    public function count($condition) {
        return $this->db->where($condition)->count();
    }

    public function GetWZ($ID) {
        $data = $this->db->field('ts_ck.*,ts_sj.CODE as SJCODE,ts_sg.CODE as SGCODE')->join('ts_sg  on ts_sg.BHCODE = ts_book.WZ_CODE')->join('ts_sj on ts_sg.SJ_ID = ts_sj.BHCODE')->join('ts_ck on ts_ck.BHCODE = ts_sj.CK_ID')->where(array('ts_book.ID' => $ID))->find();
        $data ? $str = $data['P'] . $data['C'] . $data['Z'] . $data['ADDRESS'] . $data['NAME'] . '书架代码:' . $data['SJCODE'] . '书柜代码:' . $data['SGCODE'] : $str = '书籍未入库';
        return $str;
    }

    /*
     * 微信支付
     * 支付操作
     */

    public function Pay($Odata) {
        $this->db->startTrans();
        $YE_MONEY = $this->YE_Pay($Odata['user_id'], $Odata['order_money'], $Odata['order_id'], 1);
        $where['order_id'] = $Odata['order_id'];
        $save['order_type'] = 2;
        $save['order_ye_pay_money'] = $YE_MONEY;
        $save['order_pay_time'] = date('Y-m-d H:i:s');
        $this->db = M('order');
        $OK = $this->db->where($where)->save($save);
        if ($OK && $YE_MONEY>=0) {
            $this->db = M('cost_log');
            $adddata['C_ID'] = 'CL_' . uniqid() . rand(0, 9);
            $adddata['USER_ID'] = $Odata['user_id'];
            $adddata['PAY_TYPE'] = 1;
            $adddata['MONEY'] = $Odata['order_money'] - $YE_MONEY;
            $adddata['MONEY_TYPE'] = 1;
            $adddata['SR_TYPE'] = 1;
            $adddata['CREATE_TIME'] = date('Y-m-d H:i:s');
            $adddata['STATE	'] = 1;
            $adddata['INFO'] = '微信支付';
            $adddata['OID'] = $Odata['order_id'];
            $OK1 = $this->db->add($adddata);
            if ($OK1) {
                $this->db->commit();
                return 1;
            }
        }
        $this->db->rollback();
        return 0;

    }

    /*
     * $top是否修改数据=1 修改
     * 余额支付后订单需要支付的
     * 订单金额
     * 部分余额支付
     * 返回值>=0 余额支付的金额
     * -1添加交易记录有误
     *  * -2修改余额失败
     */
    public function YE_Pay($user_id, $order_money, $order_id, $top = 0) {
        //是否有余额
        $this->db = M('cost');
        $this->db->startTrans();
        $where['USER_ID'] = $user_id;
        $CostData = $this->db->where($where)->find();
        $YE_MONEY = (float)passport_decrypt($CostData['MONEY'], C('JM_KEY'));
        if ($YE_MONEY < $order_money) {//订单金额大于余额 使用所有余额
            $save['MONEY'] = passport_encrypt('0.00', C('JM_KEY'));
            $resCost = $YE_MONEY;
        } else {
            $save['MONEY'] = passport_encrypt((String)($YE_MONEY - $order_money), C('JM_KEY'));
            $resCost = $order_money;
        }
        if ($top) {
            $save['UPDATE_TIME'] = date('Y-m-d H:i:s');
            $save['UPDATE_IP'] = get_client_ip();
            $COK = $this->db->where($where)->save($save);
            if ($COK) {
                //生成余额支付记录
                $CL_LS = M('cost_log');
                $clsave['C_ID'] = 'CL_' . uniqid() . rand(0, 99);
                $clsave['OID'] = $order_id;
                $clsave['USER_ID'] = $user_id;
                $clsave['PAY_TYPE'] = 3;
                $clsave['MONEY'] = $resCost;
                $clsave['MONEY_TYPE'] = 1;
                $clsave['SR_TYPE'] = 1;
                $clsave['CREATE_TIME'] = date('Y-m-d H:i:s');
                $clsave['STATE'] = 1;
                $clsave['INFO'] = '用户使用余额支付书，扣取用户余额';
                $CL_OK = $CL_LS->Add($clsave);
                if ($CL_OK) {
                    $this->db->commit();
                    return $resCost;
                } else {
                    $this->db->rollback();
                    return -1;//添加扣除余额记录失败
                }
            } else {
                $this->db->rollback();
                return -2;//修改余额失败
            }
        } else {
            return $resCost;//不修改数据 只返回余额支付金额
        }

    }
}


?>